package net.modul.dept.dao.impl;

import java.util.List;

import net.common.dao.GeneralDaoImpl;
import net.common.db.ConnectionManager;
import net.common.service.PageBean;
import net.modul.dept.dao.EmployeeDao;
import net.modul.dept.vo.EmployeeVo;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class EmployeeDaoImpl extends GeneralDaoImpl implements EmployeeDao {

	@Override
	public List<EmployeeVo> search(PageBean pageBean, EmployeeVo bean)
			throws Exception {
		String sql = "select empl_id,dept_id,realname,empl_num,account,password,email,com_email,call_phone,telphone,phone,sex,birth,id_card,flag,manager from t_employee where flag=0";
		if (bean.getEmpl_num() != null && !bean.getEmpl_num().equals("")) {
			sql += " and empl_num=?";
		}
		if (bean.getDept_id() != null) {
			sql += " and dept_id=?";
		}
		ResultSetHandler<List<EmployeeVo>> rsh = new BeanListHandler<EmployeeVo>(
				EmployeeVo.class);
		return gqr.querySplitPage(sql, rsh, bean, pageBean, null);
	}

	@Override
	public void insert(EmployeeVo bean) throws Exception {
		String sql = "insert into t_employee (dept_id,realname,empl_num,account,password,email,com_email,call_phone,telphone,phone,sex,birth,id_card,flag,manager) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		gqr.insert(sql, bean);
	}

	@Override
	public void update(EmployeeVo bean) throws Exception {
		String sql = "update t_employee set dept_id=?,realname=?,empl_num=?,account=?,password=?,email=?,com_email=?,call_phone=?,telphone=?,phone=?,sex=?,birth=?,id_card=? where empl_id=?";
		gqr.updateByBean(sql, bean);
	}

	@Override
	public void updateFlag(EmployeeVo bean) throws Exception {
		String sql = "update t_employee set flag=? where empl_id=?";
		gqr.updateByBean(sql, bean);
	}

	@Override
	public EmployeeVo find(Integer id) throws Exception {
		String sql = "select empl_id,dept_id,realname,empl_num,account,password,email,com_email,call_phone,telphone,phone,sex,birth,id_card,flag,manager from t_employee where empl_id=?";
		ResultSetHandler<EmployeeVo> rsh = new BeanHandler<EmployeeVo>(
				EmployeeVo.class);
		return gqr.query(ConnectionManager.getConnection(), sql, rsh, new Object[] { id });
	}
}
